<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="hotelList">
      <el-table-column width="55" align="center" />
      <!-- <el-table-column label="用户ID" align="center" prop="id" /> -->
       <el-table-column label="UID" align="center" prop="uid" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="支付金额" align="center" prop="payPrice" />
      <el-table-column label="支付时间" align="center" prop="payTime" />
      <el-table-column label="会员等级" align="center" prop="level">
        <template slot-scope="scope">
          <p v-if="scope.row.level === 1">普通会员</p>
          <p v-else>高级会员</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-circle-plus-outline"
            @click="handleUpdate(scope.row)">赠送比赛次数</el-button>
          <el-button size="mini" type="text" icon="el-icon-circle-plus-outline"
            @click="handleView(scope.row)">查看赠送详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="比赛次数" prop="giftNum">
              <el-input v-model="form.giftNum" placeholder="请输入赠送比赛次数" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="viewopen" width="800px" append-to-body>
      <el-table v-loading="loading" :data="memeberCountList">
        <el-table-column width="55" align="center" />
        <el-table-column label="次数" align="center" prop="giftNum" />
        <el-table-column label="赠送时间" align="center" prop="giftTime" />
      </el-table>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="addopen" width="600px" append-to-body>
      <el-form ref="userform" :model="userform" :rules="addRules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户ID" prop="uid">
              <el-input v-model="userform.uid" placeholder="请输入用户ID" maxlength="200" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员套餐" prop="membershipId">
              <el-select v-model="userform.membershipId" placeholder="请选择会员套餐">
                <el-option v-for="item in memberShipList" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员昵称" prop="nickName">
              <el-input v-model="userform.nickName" placeholder="请输入会员昵称" maxlength="200" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员电话" prop="phone">
              <el-input v-model="userform.phone" placeholder="请输入会员电话" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="赠送比赛次数" prop="giftNum">
              <el-input v-model="userform.giftNum" placeholder="请输入赠送比赛次数" maxlength="200" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付金额" prop="payPrice">
              <el-input v-model="userform.payPrice" placeholder="请输入会员价格" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="userform.address" placeholder="请输入地址" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addMemberUser">确 定</el-button>
        <el-button @click="addCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMemberList, setNum, getMemberCountList, getMemberShipList, addMemberUser } from "@/api/memberUser"
export default {
  name: "memberUser",
  data () {
    return {
      title: '',
      // 遮罩层
      loading: true,
      open: false,
      viewopen: false,
      addopen: false,
      memeberCountList: [],
      userform: {},
      // 选中数组
      ids: [],
      form: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户数据
      hotelList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        giftNum: [
          { required: true, message: "赠送比赛次数不能为空", trigger: "blur" }
        ]
      },
      addRules: {
        membershipId: [
          { required: true, message: "请选择会员套餐", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "请输入昵称", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入电话", trigger: "blur" }
        ],
        uid: [
          { required: true, message: "请输入uid", trigger: "blur" }
        ],
        payPrice: [
          { required: true, message: "请输入支付金额", trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入地址", trigger: "blur" }
        ]
      },
      memberShipList: [],
      imageUrl: "",
    };
  },
  created () {
    this.getList();
    this.getMemberShipList()
  },
  computed: {},
  methods: {
    getMemberShipList () {
      getMemberShipList().then((response) => {
        this.memberShipList = response.rows;
      });
    },
    handleAdd () {
      this.addopen = true
    },
    addCancel () {
      this.addopen = false
    },
    handleView (row) {
      this.viewopen = true
      this.getMemberCountList(row.id)
    },
    getMemberCountList (e) {
      getMemberCountList({ id: e }).then((response) => {
        this.memeberCountList = response.data;
        this.loading = false;
      });
    },
    /** 查询酒店列表 */
    getList () {
      this.loading = true;
      getMemberList(this.queryParams).then((response) => {
        this.hotelList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row
      this.open = true
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const currentForm = {
            id: this.form.id,
            membershipId: this.form.membershipId,
            payPrice: this.form.payPrice,
            level: this.form.level,
            giftNum: this.form.giftNum,
            memberId: this.form.id
          }
          let array = []
          array.push(currentForm)
          setNum(array).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    addMemberUser () {
      this.$refs["userform"].validate((valid) => {
        if (valid) {
          const memberShip = this.memberShipList.find(item => item.id == this.userform.membershipId)
          this.userform.level = memberShip.level
          addMemberUser(this.userform).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.addopen = false;
            this.getList();
          });
        }
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        name: undefined,
        address: undefined,
        latitude: undefined,
        longitude: undefined,
        status: false
      };
      this.imageUrl = "";
      this.resetForm("form");
    },
  },
};
</script>
<style scoped lang="scss">
.modal-wrap {
  ::v-deep .el-dialog__body {
    height: 600px;
    overflow-y: scroll;
  }
}

.avatar-uploader {
  ::v-deep .el-upload {
    width: 100px;
    height: 100px;
    border: 1px dotted #eee;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .avatar-uploader-icon {
    font-size: 24px;
  }
}

.banner-img {
  width: 100px;
  height: 100px;
  // margin-left: 20px;
}

.editBox {
  display: flex;
  justify-content: space-between;
}
</style>
